<template>
    <el-row>
        <el-form :model="login" style="width:360px;margin:7% auto;background:#fff;padding:20px;box-shadow: 0 0 50px rgba(0, 0, 0, 0.2);border-radius:5px">
            <el-row style="text-align:center;margin-bottom:10px">
                <img src="/assets/logo.png" alt="">
            </el-row>
            <el-form-item label="用户名">
                <el-input type="text" v-model="login.nickname" suffix-icon="el-icon-mobile-phone" placeholder="请输入用户名" auto-complete="off"></el-input>
            </el-form-item>
            <el-form-item label="密码">
                <el-input type="password" v-model="login.password" placeholder="请输入密码" suffix-icon="el-icon-view" auto-complete="off"></el-input>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" style="width: 100%" @click="onLogin">登录</el-button>
            </el-form-item>
        </el-form>
    </el-row>
</template>
<script>
export default {
    layout: "empty",
    data() {
        return {
            login: {
                nickname: "",
                password: "",
                captcha: "",
                captchaUuid: ""
            },
            captchaSvg: ""
        };
    },
    mounted() {
        this.$cookies.remove("Token", {
            path: "/",
            domain: this.getCoryright()
        });
    },
    methods: {
        async onLogin() {
            if (!this.login.nickname) {
                this.$message.error("请输入用户名");
                return;
            }
            if (!this.login.password) {
                this.$message.error("请输入密码");
                return;
            }

            const login = await this.$axios.$post("/api/admin/public/login", {
                nickname: this.login.nickname,
                password: this.login.password
            });
            if (login && login.errmsg === "ok") {
                this.$message({
                    message: "登录成功",
                    type: "success"
                });
                this.$cookies.set("admin", this.login.nickname, {
                    path: "/",
                    domain: this.getCoryright(),
                    maxAge: 60 * 60 * 24 * 7
                });
                this.$cookies.set("Token", login.data.token, {
                    path: "/",
                    domain: this.getCoryright(),
                    maxAge: 60 * 60 * 24 * 7
                });

                const res = await this.$axios.$get("/api/admin/table/index");
                if (res && res.errmsg === "ok") {
                    this.$router.push(`/admin/table/${res.data[0].Name}`);
                    this.$cookies.set("header", res.data[0].Name, {
                        path: "/",
                        domain: this.getCoryright(),
                        maxAge: 60 * 60 * 24 * 7
                    });
                }
            }
        }
    }
};
</script>

